import axios from 'axios'
 
axios.defaults.timeout = 10000 // 请求超时时间

//请求域名
const baseURL = 'http://localhost:8888'
 
const httpService = axios.create({
    baseURL: baseURL,
})
 
// axios 请求拦截器
httpService.interceptors.request.use(
    config=>{
        if(localStorage.getItem("access_token")){
			config.headers.Authorization = 'Bearer' + '  ' + localStorage.getItem('access_token');
        }
        return config
    },error=>{
        return Promise.reject(error)
    }
) 

// axios respone拦截器
httpService.interceptors.response.use(
    res=>{
        if(res.status == 200){
            return res;
        }else if (res.status == 401){
            return res;
        }else if (res.status == 201) {
            return res;
        }
        return res;
    },
    error=>{
        const responseCode = error.response.status;
        switch (responseCode) {
        case 400:
            message.error('请求错误(400)')
            break
        case 401:
            message.error('未授权，请重新登录(401)')
            break
        case 403:
            message.error('拒绝访问(403)')
            break
        case 404:
            message.error('请求出错(404)')
            break
        case 408:
            message.error('请求超时(408)')
            break
        case 500:
            message.error('服务器错误(500)')
            break
        case 501:
            message.error('服务未实现(501)')
            break
        case 502:
            message.error('网络错误(502)')
            break
        case 503:
            message.error('服务不可用(503)')
            break
        case 504:
            message.error('网络超时(504)')
            break
        case 505:
            message.error('HTTP版本不受支持(505)')
            break
        default:
            message.error(`连接出错(${responseCode})!`)
        }
        return Promise.reject(error.response.data)
    }
)
 
export default httpService;